package com.meta.platform.login.plus.vcode.rest;

import javax.annotation.Resource;

import com.meta.act.app.service.ISysUserService;
import com.meta.framework.common.annotation.Log;
import com.meta.framework.common.enums.BusinessType;
import com.meta.framework.common.exception.CustomException;
import com.meta.framework.common.utils.SecurityUtils;
import com.meta.platform.login.plus.common.vo.LoginSuccessVo;
import com.meta.platform.login.plus.vcode.domain.VCodeSendRecord;
import com.meta.platform.login.plus.vcode.rest.req.*;
import com.meta.platform.login.plus.vcode.service.VCodeService;
import com.meta.platform.login.plus.vcode.service.VCodeUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.meta.framework.common.core.controller.BaseController;
import com.meta.framework.core.CommonRes;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 验证码 Rest API
 *
 * @author pengjianfeng
 * @version 1.0
 * @date 2021/1/11 3:02 下午
 */
@Api
@RestController
@RequestMapping("/vCode")
public class VCodeController extends BaseController {
    
    @Resource
    private VCodeService vcodeService;

    @Autowired
    private ISysUserService sysUserService;
    
    @Resource
    private com.meta.platform.login.plus.vcode.service.VCodeUserService VCodeUserService;
    
    /**
     * 获取验证码
     *
     * @param getVcodeCmd 请求参数
     * @return
     */
    @PostMapping
    @ApiOperation("获取验证码")
    public CommonRes get(@RequestBody GetVCodeCmd getVcodeCmd) {
        VCodeSendRecord vCode = this.vcodeService.getVCode(getVcodeCmd);
        return CommonRes.success(vCode);
    }
    
    /**
     * 验证码登录
     *
     * @param loginCmd
     */
    @PostMapping("/login")
    @ApiOperation("通过验证码登录")
    public CommonRes login(@RequestBody VCodeLoginCmd loginCmd) {
        return CommonRes.success(vcodeService.login(loginCmd));
    }

    /**
     * 重置密码
     */
    @Log(title = "个人信息", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPwd")
    public CommonRes resetPwd(VCodeResetPwdCmd vCodeResetPwdCmd)
    {
        boolean verified = vcodeService.verifyVCode(vCodeResetPwdCmd.getTelephone(), vCodeResetPwdCmd.getVCode());
        if (!verified) {
            throw new CustomException("验证码错误");
        }
        if (sysUserService.resetUserPwd(vCodeResetPwdCmd.getTelephone(), SecurityUtils.encryptPassword(vCodeResetPwdCmd.getNewPassword())) > 0)
        {
            return CommonRes.success();
        }
        return CommonRes.error("修改密码异常，请联系管理员");
    }
    
    /**
     * 账号注册
     *
     * @param registerCmd 注册参数
     * @return
     */
    @PostMapping("register")
    @ApiOperation("通过验证码注册账号")
    public CommonRes register(@RequestBody RegisterCmd registerCmd) {
        LoginSuccessVo loginSuccessVo = VCodeUserService.register(registerCmd);
        return null != loginSuccessVo ? CommonRes.success(loginSuccessVo) : CommonRes.error();
    }
    
    /**
     * 找回密码
     *
     * @param findPasswordCmd 参数
     * @return
     */
    @PostMapping("findPwd")
    @ApiOperation("通过验证码找回密码")
    public CommonRes findPassword(@RequestBody FindPasswordCmd findPasswordCmd) {
        return VCodeUserService.findPassword(findPasswordCmd) > 0 ? CommonRes.success() : CommonRes.error();
    }
}
